<template>
    <div class="shop-info-container">
      <h1>店铺基本信息</h1>
      <el-form ref="shopInfoForm" :model="shopInfoForm" label-width="120px" class="shop-info-form">
        <el-form-item label="店铺ID" prop="shopId">
          <el-input v-model="shopInfoForm.shopId" placeholder="请输入店铺ID" disabled></el-input>
        </el-form-item>
        <el-form-item label="店铺名" prop="shopName">
          <el-input v-model="shopInfoForm.shopName" placeholder="请输入店铺名" disabled></el-input>
        </el-form-item>
        <el-form-item label="店铺地址" prop="shopAddress">
          <el-input v-model="shopInfoForm.shopAddress" placeholder="请输入店铺地址" disabled></el-input>
        </el-form-item>
        <el-form-item label="店铺电话" prop="shopPhone">
          <el-input v-model="shopInfoForm.shopPhone" placeholder="请输入店铺电话" disabled></el-input>
        </el-form-item>
        <el-form-item label="店铺开始时间" prop="shopStartTime">
          <el-time-picker 
            v-model="shopInfoForm.shopStartTime" 
            placeholder="选择店铺开始时间" 
            format="HH:mm" 
            value-format="HH:mm">
          </el-time-picker>
        </el-form-item>
        <el-form-item label="店铺结束时间" prop="shopEndTime">
          <el-time-picker 
            v-model="shopInfoForm.shopEndTime" 
            placeholder="选择店铺结束时间" 
            format="HH:mm" 
            value-format="HH:mm">
          </el-time-picker>
        </el-form-item>
        <el-form-item label="是否关闭">
          <el-switch v-model="shopInfoForm.isClosed" @change="handleSwitchChange" active-text="已关闭" inactive-text="开放"></el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitShopInfo">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </template>
  
  <script>
  export default {
    data() {
      return {
        shopInfoForm: {
          shopId: null,
          shopName: '',
          shopAddress: '',
          shopPhone: '',
          shopStartTime: '09:00',
          shopEndTime: '18:00',
          isClosed: 0
        }
      };
    },
    methods: {
      checkTime() {
        const startTime = new Date(`1970-01-01T${this.shopInfoForm.shopStartTime}:00`);
        const endTime = new Date(`1970-01-01T${this.shopInfoForm.shopEndTime}:00`);
        if (startTime >= endTime) {
          this.$message.error('结束时间必须晚于开始时间');
          return false;
        }
        return true;
      },
      submitShopInfo() {
        if (!this.checkTime()) {
          return;
        }
        this.$refs.shopInfoForm.validate((valid) => {
          if (valid) {
            this.$http.put("/market/shopInfo/updateNoAudit", this.shopInfoForm)
              .then(resp => {
                if (resp.data.code === 200) {
                  this.$message.success(resp.data.message);
                } else {
                  this.$message.error(resp.data.message);
                }
              })
              .catch(error => {
                console.error('提交失败:', error);
                this.$message.error('提交失败，请稍后再试');
              });
          } else {
            console.error('表单验证失败');
          }
        });
      },
      findShopInfo() {
        this.$http.post("/market/shopInfo/getShopInfo")
          .then(res => {
            console.log(res.data.data); // 调试输出
            this.shopInfoForm = {
              ...res.data.data,
              shopStartTime: res.data.data.shopStartTime || '09:00',
              shopEndTime: res.data.data.shopEndTime || '18:00',
            };
          })
          .catch(error => {
            console.error('获取店铺信息失败:', error);
            this.$message.error('获取店铺信息失败，请稍后再试');
          });
      },
      resetForm() {
        this.shopInfoForm = {
          shopId: null,
          shopName: '',
          shopAddress: '',
          shopPhone: '',
          shopStartTime: '09:00',
          shopEndTime: '18:00',
          isClosed: 0
        };
      },
      handleSwitchChange(value) {
const status = value ? 1 : 0; // 判断开关状态
let data = new FormData();
data.append("isClosed", status);
this.$http.put("/market/shopInfo/isClosed", data)
.then(response => {
if (response.data.code === 200) {
this.$message.success(response.data.message);
} else {
this.$message.error(response.data.message);
}
console.log('状态更新成功:', response.data);
})
.catch(error => {
console.error('状态更新失败:', error);
});
},
    },
    created() {
      this.findShopInfo();
    }
  };
  </script>
  
  <style scoped>
  .shop-info-container {
    padding: 20px;
  }
  .shop-info-form {
    max-width: 600px;
    margin: auto;
  }
  </style>